﻿// CFlowCaliDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "CFlowCaliDlg.h"
#include "afxdialogex.h"


// CFlowCaliDlg 对话框

IMPLEMENT_DYNAMIC(CFlowCaliDlg, CDialogEx)

CFlowCaliDlg::CFlowCaliDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_FLOWCALIDLG, pParent)
{
	m_nCurIndex = 0;
	m_nCurID = -1;
}

CFlowCaliDlg::~CFlowCaliDlg()
{
}

void CFlowCaliDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_C1, m_dC[0]);
	DDX_Text(pDX, IDC_C2, m_dC[1]);
	DDX_Text(pDX, IDC_C3, m_dC[2]);
	DDX_Text(pDX, IDC_C4, m_dC[3]);
	DDX_Text(pDX, IDC_C5, m_dC[4]);
	DDX_Text(pDX, IDC_C6, m_dC[5]);
	DDX_Text(pDX, IDC_C7, m_dC[6]);
	DDX_Text(pDX, IDC_C8, m_dC[7]);
	DDX_Text(pDX, IDC_C9, m_dC[8]);
	DDX_Text(pDX, IDC_C10, m_dC[9]);
	DDX_Text(pDX, IDC_CA1, m_dArea[0]);
	DDX_Text(pDX, IDC_CA2, m_dArea[1]);
	DDX_Text(pDX, IDC_CA3, m_dArea[2]);
	DDX_Text(pDX, IDC_CA4, m_dArea[3]);
	DDX_Text(pDX, IDC_CA5, m_dArea[4]);
	DDX_Text(pDX, IDC_CA6, m_dArea[5]);
	DDX_Text(pDX, IDC_CA7, m_dArea[6]);
	DDX_Text(pDX, IDC_CA8, m_dArea[7]);
	DDX_Text(pDX, IDC_CA9, m_dArea[8]);
	DDX_Text(pDX, IDC_CA10, m_dArea[9]);
}


BEGIN_MESSAGE_MAP(CFlowCaliDlg, CDialogEx)
	ON_BN_CLICKED(ID_APPLY, &CFlowCaliDlg::OnBnClickedApply)
	ON_BN_CLICKED(IDCANCEL, &CFlowCaliDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BTPRE, &CFlowCaliDlg::OnBnClickedBtpre)
	ON_BN_CLICKED(IDC_BTNEXT, &CFlowCaliDlg::OnBnClickedBtnext)
	ON_BN_CLICKED(IDC_BTSORT, &CFlowCaliDlg::OnBnClickedBtsort)
END_MESSAGE_MAP()


// CFlowCaliDlg 消息处理程序


BOOL CFlowCaliDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	GetData();
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CFlowCaliDlg::OnBnClickedApply()
{
	// TODO: 在此添加控件通知处理程序代码
	SetData();
}


void CFlowCaliDlg::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnCancel();
}


void CFlowCaliDlg::OnBnClickedBtpre()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_nCurIndex > 0)
	{
		m_nCurIndex--;
		GetData();
	}
}


void CFlowCaliDlg::OnBnClickedBtnext()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_nCurIndex < 8)
	{
		m_nCurIndex++;
		GetData();
	}
}


void CFlowCaliDlg::OnBnClickedBtsort()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);

	for (int i = 0; i < 9; i++)
	{
		double dMin = m_dC[i];
		int		nMinIndex = i;
		for (int k = i + 1; k < 10; k++)
		{
			if (dMin > m_dC[k])
			{
				dMin = m_dC[k];
				nMinIndex = k;
			}
		}
		if (nMinIndex != i)
		{
			dMin = m_dC[i];
			m_dC[i] = m_dC[nMinIndex];
			m_dC[nMinIndex] = dMin;
			dMin = m_dArea[i];
			m_dArea[i] = m_dArea[nMinIndex];
			m_dArea[nMinIndex] = dMin;
		}
	}
	//处理一下0，0的问题
	int nIndex = 0;
	for (int i = 0; i < 10; i++)
	{
		if (m_dC[i] == 0)
			nIndex = i;
		else
			break;
	}
	//移位
	for (int i = nIndex; i < 10; i++)
	{
		double tt = m_dC[i];
		m_dC[i - nIndex] = m_dC[i];
		tt = m_dArea[i];
		m_dArea[i - nIndex] = m_dArea[i];
	}
	for (int i = 0; i < nIndex; i++)
	{
		m_dC[9 - i] = 0;
		m_dArea[9 - i] = 0;
	}
	UpdateData(FALSE);
}

void CFlowCaliDlg::GetData()
{
	m_bModify = FALSE;
	for (int i = 0; i < 10; i++)
	{
		m_dC[i] = gl.gcMachine.sFlow[m_nCurIndex].dP[i];
		m_dArea[i] = gl.gcMachine.sFlow[m_nCurIndex].dF[i];
	}
	switch (m_nCurIndex)
	{
	case 0:
		GetDlgItem(IDC_NAME)->SetWindowText(_T("组份A空气"));
		break;
	case 1:
		GetDlgItem(IDC_NAME)->SetWindowText(_T("组份A预切"));
		break;
	case 2:
		GetDlgItem(IDC_NAME)->SetWindowText(_T("组份A主柱"));
		break;
	case 3:
		GetDlgItem(IDC_NAME)->SetWindowText(_T("组份B空气"));
		break;
	case 4:
		GetDlgItem(IDC_NAME)->SetWindowText(_T("组份B预切"));
		break;
	case 5:
		GetDlgItem(IDC_NAME)->SetWindowText(_T("组份B主柱"));
		break;
	case 6:
		GetDlgItem(IDC_NAME)->SetWindowText(_T("全烃空气"));
		break;
	case 7:
		GetDlgItem(IDC_NAME)->SetWindowText(_T("全烃氢气"));
		break;
	case 8:
		GetDlgItem(IDC_NAME)->SetWindowText(_T("样品气"));
		break;
	}
	UpdateData(FALSE);
}

void CFlowCaliDlg::SetData()
{
	m_bModify = TRUE;
	UpdateData(TRUE);
	for (int i = 0; i < 10; i++)
	{
		gl.gcMachine.sFlow[m_nCurIndex].dP[i] = m_dC[i];
		gl.gcMachine.sFlow[m_nCurIndex].dF[i] = m_dArea[i];
	}
}
